package com.bw.a2209clubshop.ui.mall.adapter

import android.content.Context
import android.widget.ImageView
import androidx.databinding.ViewDataBinding
import com.bumptech.glide.Glide
import com.bw.a2209clubshop.R
import com.bw.a2209clubshop.databinding.ItemMallHomeBannerBinding
import com.bw.a2209clubshop.databinding.ItemMallHomeGoodsBinding
import com.bw.a2209clubshop.databinding.ItemMallHomeRowTitleBinding
import com.bw.a2209clubshop.ui.mall.model.MallBanner
import com.bw.a2209clubshop.ui.mall.model.MallModel
import com.bw.a2209clubshop.ui.mall.model.MallRowTypes
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.youth.banner.loader.ImageLoader

class MallHomeMultiAdapter(

//    private val onClick: ((tag: Int) -> Unit)?,
//    private val onBookingClick: ((MallBanner) -> Unit)? = null
): BaseMultiItemQuickAdapter<MallModel,BaseDataBindingHolder<ViewDataBinding>>() {

    init {
        addItemType(MallRowTypes.BANNER, R.layout.item_mall_home_banner)
        addItemType(MallRowTypes.TITLE, R.layout.item_mall_home_row_title)
        addItemType(MallRowTypes.GOODS, R.layout.item_mall_home_goods)
    }
    override fun convert(holder: BaseDataBindingHolder<ViewDataBinding>, item: MallModel) {
        when(item.itemType){
            MallRowTypes.TITLE -> {
                val binding = holder.dataBinding as ItemMallHomeRowTitleBinding
                convertTitle(binding, item)
            }
            MallRowTypes.BANNER -> {
                val binding = holder.dataBinding as ItemMallHomeBannerBinding
                convertBanner(binding, item)
            }
            MallRowTypes.GOODS -> {
                val binding = holder.dataBinding as ItemMallHomeGoodsBinding
                convertGoods(binding, item)
            }
        }
    }
    private fun convertGoods(binding: ItemMallHomeGoodsBinding, item: MallModel) {
        item.goodsList?.forEach {
            binding.item = it
            binding.commonPrice.tvPrice.text=it.purchasePrice.toString()
//            binding.commonPrice.item=it
            Glide.with(context).load(it.mainImgUrl).into(binding.ivGoodsPic)
        }
        // 查看商品详情
//        binding.root.setOnClickListener {
//            GoodsDetailActivity.jump(item.goods?.id ?: 0, false)
//        }
    }
    private fun convertBanner(binding: ItemMallHomeBannerBinding, item: MallModel) {
        val banners:ArrayList<String> = ArrayList()
        item.bannerList?.forEach {
            banners.add(it.imageUrl)
        }
        binding.mallBanner.setImages(banners)
        binding.mallBanner.setImageLoader(object : ImageLoader(){
            override fun displayImage(context: Context?, path: Any?, imageView: ImageView?) {
                Glide.with(context!!).load(path).into(imageView!!)
            }
        })
        binding.mallBanner.start()
//        val bannerCount = banners?.size ?: 0

    }
    private fun convertTitle(binding: ItemMallHomeRowTitleBinding, item: MallModel) {
        item.titleList?.forEach {
            binding.item=it
        }

//        binding.btnJumpMore.setOnClickListener {
//            when (item.tag) {
//                MallRowTags.ACTIVITY -> {// 更多活动
//                    ActListActivity.jump()
//                }
//                MallRowTags.GOODS -> {// 更多商品
//                    MallGroupGoodsListActivity.jump(
//                        groupId = item.id,
//                        typeName = item.title ?: "商品列表",
//                        fromHome = true
//                    )
//                }
//                else -> {
//                    onClick?.invoke(item.tag ?: "")
//                }
//            }
//        }
    }

}